#include "StdAfx.h"
#include "HMAC.h"

namespace Crypt {

HMAC::HMAC()
{
}

void HMAC::Initialize(uint32 len, uint8* seed)
{
        HMAC_CTX_init(&mC);
        HMAC_Init_ex(&mC, seed, len, EVP_sha1(), NULL);
}

HMAC::~HMAC()
{
    HMAC_CTX_cleanup(&mC);
}

void HMAC::UpdateBigNumber(BigNumber* bn0, ...)
{
        va_list v;
        BigNumber *bn;

        va_start(v, bn0);
        bn = bn0;
        while(bn)
        {
                UpdateData(bn->AsByteArray(), bn->GetNumBytes());
                bn = va_arg(v, BigNumber *);
        }
        va_end(v);
}

void HMAC::UpdateData(const uint8* data, int32 length)
{
    HMAC_Update(&mC, data, length);
}

void HMAC::UpdateData(const std::string& str)
{
        UpdateData((uint8*)str.c_str(), (int32)str.length());
}

void HMAC::Finalize()
{
    uint32 length = 0;
    HMAC_Final(&mC, (uint8*)mDigest, (uint32*)&length);
}

};